package com.example.ending_demo.service;

import com.example.ending_demo.dao.AddressDao;
import com.example.ending_demo.dao.SearchDao;
import com.example.ending_demo.model.PageInfo;
import com.example.ending_demo.model.Address;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressService {
    public int addAddress(Address address) throws SQLException {
        AddressDao dao = new AddressDao();
        int result = dao.addAddress(address);
        return result;
    }

    public List<Address> getAddressByUserID(int user_id){
        AddressDao dao = new AddressDao();
        ArrayList addrList = (ArrayList) dao.getAddressByUserID(user_id);
        return  addrList;
    }

    //分页查询通讯
    public PageInfo<Address> selectPage(Integer pageNo,Integer pageSize,Integer user_id){
        AddressDao dao = new AddressDao();
        PageInfo<Address> pageInfo = new PageInfo<Address>();
        pageInfo.setPageSize(pageSize);
        Integer pageTotalCount = dao.queryForPageTotalCount(user_id);
        pageInfo.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal += 1;
        }
        pageInfo.setPageTotal(pageTotal);
        pageInfo.setPageNo(pageNo);

        int begin = (pageInfo.getPageNo()-1)*pageSize;
        if(begin<0){
            begin = 1;
        }
        //这里直接写死了userid;
        List<Address> items = dao.queryForPageItems(begin,pageSize,user_id);
        pageInfo.setItems(items);

        return pageInfo;
    }

    //搜索结果插入数据库分页查询
    public PageInfo<Address> searchPage(Integer pageNo,Integer pageSize, String searchStr, Integer user_id){
        SearchDao dao = new SearchDao();
        dao.searchAdd(searchStr,user_id);
        PageInfo<Address> pageInfo = new PageInfo<Address>();
        pageInfo.setPageSize(pageSize);
        Integer pageTotalCount = dao.queryForPageTotalCount();
        pageInfo.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal += 1;
        }
        pageInfo.setPageTotal(pageTotal);
        pageInfo.setPageNo(pageNo);

        int begin = (pageInfo.getPageNo()-1)*pageSize;

        List<Address> items = dao.queryForPageItems(begin,pageSize);
        pageInfo.setItems(items);

        return pageInfo;
    }

    //搜索查到结果之后分页
    public PageInfo<Address> selectDonePage(Integer pageNo,Integer pageSize){
        SearchDao dao = new SearchDao();
        PageInfo<Address> pageInfo = new PageInfo<Address>();
        pageInfo.setPageSize(pageSize);
        Integer pageTotalCount = dao.queryForPageTotalCount();
        pageInfo.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount/pageSize;
        if(pageTotalCount/pageSize>0){
            pageTotal += 1;
        }
        pageInfo.setPageTotal(pageTotal);
        pageInfo.setPageNo(pageNo);

        int begin = (pageInfo.getPageNo()-1)*pageSize;
        //这里直接写死了userid;
        List<Address> items = dao.queryForPageItems(begin,pageSize);
        pageInfo.setItems(items);

        return pageInfo;
    }

    public Address getAddressByAddressID(int addr_id) throws SQLException {
        AddressDao dao = new AddressDao();
        return dao.getAddressByAddressID(addr_id);
    }

    public int updateAddress(Address addrObj) throws SQLException {
        AddressDao dao = new AddressDao();
        return dao.updateAddress(addrObj);
    }
}
